var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin')

// 环境变量配置，dev / online
var WEBPACK_ENV = process.env.WEBPACK_ENV || 'dev';

//获取html-webpack-plugin参数的方法
var getHtmlConfig = function(name,title){
	return {
		//配置模板的路径
		template: './src/view/' + name + '.html',
		//放的是目标文件的位置，根据output里的path作为相对路径
		filename: 'view/' + name + '.html',
		//讲所有资产注入给定template，当传递true或'body'所有js资源将被防止在正文元素的底部，'head'将放置在head元素中
		title: title,
		inject: true,
		//如果true将webpack所有包含的脚本和css文件附加一个独特的编译哈希，这对缓存清除非常有用
		hash: true,
		// 需要打包的模块，允许你值添加一些块（例如只有单元测试块）
		chunks: ['common',name]
	}
}

var config = {
	entry: {     //多入口配置
		'common': ['./src/page/common/index.js'],  //公共模块
		'index': ['./src/page/index/index.js'],
		'login': ['./src/page/login/index.js'],
		'result': ['./src/page/result/index.js'],
	},
	output: {
		path: path.resolve(__dirname,'./dist'),
		publicPath: '/dist',
		filename:'js/[name].js'
	},
	externals: {
		'jquery': 'window.jQuery'
	},
	module: {
    loaders: [
      { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") },
      { test: /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/, loader: 'url-loader?limit=1024&name=resource/[name].[ext]' },
      { test: /\.string$/, loader: 'html-loader'}
    ]
  },
  resolve: {
  	alias : {
      node_modules    : __dirname + '/node_modules',
      util            : __dirname + '/src/util',
      page            : __dirname + '/src/page',
      service         : __dirname + '/src/service',
      image           : __dirname + '/src/image'
  	}
  },
	plugins: [
		//独立通用模块到js/base.js
		new webpack.optimize.CommonsChunkPlugin({    //公共（通用）模块配置到一起
			name: 'common',
			filename: 'js/base.js'
		}),
		//把css单独打包到文件里
		new ExtractTextPlugin("css/[name].css"),
		//html模板的处理
		new HtmlWebpackPlugin(getHtmlConfig('index','首页')),
		new HtmlWebpackPlugin(getHtmlConfig('login','登陆')),
		new HtmlWebpackPlugin(getHtmlConfig('result','操作结果')),
	]
};

if('dev' === WEBPACK_ENV){
    config.entry.common.push('webpack-dev-server/client?http://localhost:8088/');
}

module.exports = config;